export * from './request'
export * from './storage'
export * from './validate'
export * from './format'
export * from './system'
export * from './message'
export * from './streamChat'

// 防抖函数 (保持不变)
export function debounce(fn, delay = 500) {
  let timer
  return function(...args) {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      fn.apply(this, args)
    }, delay)
  }
}

// 节流函数 (保持不变)
export function throttle(fn, delay = 500) {
  let last = 0
  return function(...args) {
    const now = Date.now()
    if (now - last > delay) {
      last = now
      fn.apply(this, args)
    }
  }
}

// 页面跳转更新
export function navigate(options = {}) {
  const { 
    url, 
    type = 'navigateTo',
    delta = 1,
    success,
    fail,
    complete
  } = options

  if (!url && type !== 'navigateBack') return

  const events = {
    success,
    fail: (err) => {
      console.error(`Navigation failed: ${err}`)
      fail?.(err)
    },
    complete
  }

  // 页面路由监听
  uni.onAppRoute(({ path, query }) => {
    console.log('Route changed:', { path, query })
  })

  // 页面动画结束监听
  uni.onAppRouteDone(() => {
    console.log('Route animation done')
  })

  switch(type) {
    case 'navigateTo':
      return uni.navigateTo({ url, ...events })
    case 'redirectTo':
      return uni.redirectTo({ url, ...events })
    case 'reLaunch':
      return uni.reLaunch({ url, ...events })
    case 'switchTab':
      return uni.switchTab({ url, ...events })
    case 'navigateBack':
      return uni.navigateBack({ 
        delta,
        ...events
      })
  }
}

// 复制文本更新
export function copyText(text) {
  return new Promise((resolve, reject) => {
    // 获取剪贴板数据
    uni.getClipboardData({
      success: (res) => {
        // 设置剪贴板数据
        uni.setClipboardData({
          data: text,
          success: () => {
            resolve(true)
            uni.showToast({
              title: '复制成功',
              icon: 'success'
            })
          },
          fail: reject
        })
      },
      fail: reject
    })
  })
}

// 新增 EventChannel 通信方法
export function createEventChannel() {
  const eventChannel = new EventChannel()
  return eventChannel
}

// 新增路由状态获取
export function getRouteInfo() {
  return {
    route: uni.router?.getRouteContext?.(),
    options: uni.getLaunchOptionsSync()
  }
}

// 新增页面滚动方法
export function pageScrollTo(options) {
  return uni.pageScrollTo({
    scrollTop: 0,
    duration: 300,
    ...options
  })
}